package myHive;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;



public class myHive {

	public static void main(String[] args) {
		try {
			Class.forName("org.apache.hive.jdbc.HiveDriver");
			Connection conn = DriverManager.getConnection("jdbc:hive2://192.168.192.101:10000/default", "hue", "hue");
			Statement stmt=conn.createStatement();
			stmt.execute("set hive.cli.print.header=true");
			stmt.execute("set hive.resultset.use.unique.column.names=false");
			PreparedStatement prestmt = conn.prepareStatement("SELECT  * FROM myhive.netlogs where logip like '192.168.109%' order by logtime desc  LIMIT 1000 offset 0");
			ResultSet res = prestmt.executeQuery();
			int columnCount = res.getMetaData().getColumnCount();
			for (int i = 1; i <= columnCount; i++) {

				System.out.print(res.getMetaData().getColumnLabel(i));
				if(i<columnCount){
					System.out.print(",");
				}
			}
			System.out.println();
			for (int i = 1; i <= columnCount; i++) {

				System.out.print(res.getMetaData().getColumnTypeName(i));
				if(i<columnCount){
					System.out.print(",");
				}
			}
			System.out.println();

			while (res.next()) {
				for (int i = 1; i <= columnCount; i++) {
						System.out.print(res.getObject(res.getMetaData().getColumnName(i)));
						if(i<columnCount){
							System.out.print(",");
						}
				}


				System.out.println();
			}
			ResultSet resCount=stmt.executeQuery("select count(*) from myhive.netlogs");
			resCount.next();
			System.out.println(resCount.getInt(1));
			resCount.close();
			res.close();
			prestmt.close();
			conn.close();
			// TODO Auto-generated method stub

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
